package com.example.exam.sysResource.service;

import com.example.exam.common.ResultBean;
import com.example.exam.common.ResultBeanCodeEnum;
import com.example.exam.sysDictInfo.dao.SysDictInfoDao;
import com.example.exam.sysDictInfo.model.SysDictInfoBean;
import com.example.exam.sysPermission.dao.SysPermissionDao;
import com.example.exam.sysPermission.entity.SysPermissionBean;
import com.example.exam.sysResource.dao.SysResourceDao;
import com.example.exam.sysResource.model.SysResourceBean;
import com.example.exam.sysRoleResourcePermission.dao.SysRoleResourcePermissionDao;
import com.example.exam.sysRoleResourcePermission.entity.SysRoleResourcePermissionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
public class SysResourceService {
    @Autowired
    SysResourceDao sysResourceDao;
    @Autowired
    private SysDictInfoDao dictInfoDao;
    @Autowired
    private SysPermissionDao sysPermissionDao;
    @Autowired
    private SysRoleResourcePermissionDao sysRoleResourcePermissionDao;
    /**
     * @Description:
     * @Param: [id]
     * @return: com.example.exam.common.ResultBean
     * @Author: xhw
     * @Date: 2021/7/26
     */public ResultBean getResourceList(Integer id) {
             ResultBean resultBean = new ResultBean();
        try {
            List<Map<String, Object>> list = sysResourceDao.getResourceList(id);
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            resultBean.fillData(list);
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return resultBean;
    }
    /**
     * @Description:
     * @Param: [sysResourceBean]
     * @return: com.example.exam.common.ResultBean
     * @Author: xhw
     * @Date: 2021/7/26
     */public ResultBean insertResource(SysResourceBean sysResourceBean){
             ResultBean resultBean = new ResultBean();
        try {
            if (!ObjectUtils.isEmpty(sysResourceBean.getPid())) {
                sysResourceBean.setType(2);
                sysResourceBean.setSeq(sysResourceDao.queryMaxSeq(sysResourceBean.getPid()) + 1);
            } else {
                sysResourceBean.setSeq(sysResourceDao.queryMaxSeq(0) + 1);
                sysResourceBean.setType(1);
                sysResourceBean.setPid(0);
            }
            sysResourceBean.setCreateTime(new Date());
            sysResourceBean.setStatus(1);
            int i = sysResourceDao.add(sysResourceBean);
            if (i != 0) {
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            } else {
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return resultBean;
    }
    /**
     * @Description:
     * @Param: [id]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */ public Integer deleteResource(Integer id){
        Integer i=sysResourceDao.deleteResource(id);
        if(i>0){
            return 1;
        }else{
            return -1;
        }
    }/**
     * @Description:
     * @Param: [sysResourceBean]
     * @return: java.lang.Integer
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public Integer updateResource(SysResourceBean sysResourceBean){
        Integer i=sysResourceDao.updateResource(sysResourceBean);
        if(i>0){
            return 1;
        }else{
            return -1;
        }
    }/**
     * @Description:
     * @Param: [id]
     * @return: com.example.exam.sysResource.model.SysResourceBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public SysResourceBean selectOne(Integer id){
        SysResourceBean sysResourceBean=sysResourceDao.findById(id);
        return sysResourceBean;
    }/**
     * @Description:查询子菜单
     * @Param: [pid]
     * @return: java.util.List<com.example.exam.sysResource.model.SysResourceBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysResourceBean> queryResource(int pid) {
             return sysResourceDao.queryResource(pid);
    }/**
     * @Description:
     * @Param: []
     * @return: java.util.List<com.example.exam.sysResource.model.SysResourceBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysResourceBean> queryParentResource() {
        return sysResourceDao.queryParentResource();
    }
    /**
     * @Description:
     * @Param: [id]
     * @return: com.example.exam.sysResource.model.SysResourceBean
     * @Author: xhw
     * @Date: 2021/7/26
     */public SysResourceBean findById(Integer id) {
             return sysResourceDao.findById(id);
    }/**
     * @Description:
     * @Param: [id]
     * @return: java.util.List<com.example.exam.sysDictInfo.model.SysDictInfoBean>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<SysDictInfoBean> queryChild(Integer id) {
         List<SysDictInfoBean> list;
        try {
            list = dictInfoDao.queryChild(id);
        } catch (Exception e) {
            e.printStackTrace();
            list = null;
        }
        return list;
    }
    /**
     * @Description:
     * @Param: [roleId]
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public List<Map<String, Object>> getMenuListByRoleId(String roleId) {
        try {
            List<SysResourceBean> pMenuList = sysResourceDao.queryParentResource();
            if(pMenuList!=null&&pMenuList.size()>0) {
                List<Map<String, Object>> tree = new ArrayList<Map<String, Object>>();
                for (int i = 0; i < pMenuList.size(); i++) {
                    SysResourceBean pBean = pMenuList.get(i);
                    Map<String, Object> nodes = new HashMap<String, Object>();
                    nodes.put("id", pBean.getId());
                    nodes.put("title", pBean.getName());
                    nodes.put("parentId", "0");
                    int isPCheck = 0;
                    List<SysResourceBean> cMenuList = sysResourceDao.queryResource(pBean.getId());
                    if(cMenuList!=null&&cMenuList.size()>0) {
                        List<Map<String, Object>> ctree = new ArrayList<Map<String, Object>>();
                        for (int j = 0; j < cMenuList.size(); j++) {
                            Map<String, Object> cnodes = new HashMap<String, Object>();
                            SysResourceBean cBean = cMenuList.get(j);
                            cnodes.put("id", cBean.getId());
                            cnodes.put("title", cBean.getName());
                            cnodes.put("parentId", pBean.getId());
                            Integer isCPCheck = 0;
                            List<SysPermissionBean> permissionList = sysPermissionDao.queryPermissionList(cBean.getId());
                            if(permissionList!=null&&permissionList.size()>0) {
                                List<Map<String, Object>> dtree = new ArrayList<Map<String, Object>>();
                                for (int k = 0; k < permissionList.size(); k++) {
                                    SysPermissionBean bean = permissionList.get(k);
                                    Map<String, Object> dnodes = new HashMap<String, Object>();
                                    dnodes.put("id", bean.getId());
                                    dnodes.put("title", bean.getName());
                                    dnodes.put("parentId", cBean.getId());
                                    SysRoleResourcePermissionBean haPermissionBean  = sysRoleResourcePermissionDao.queryDataByRrp(roleId,cBean.getId(),bean.getId());
                                    if(haPermissionBean!=null) {
                                        dnodes.put("checkArr", "1");
                                        isCPCheck++;
                                    }else {
                                        dnodes.put("checkArr", "0");
                                    }
                                    dtree.add(dnodes);
                                }
                                if(isCPCheck==0) {
                                    cnodes.put("checkArr", "0");
                                }else {
                                    cnodes.put("checkArr", "1");
                                    isPCheck++;
                                }
                                cnodes.put("children", dtree);
                            }
                            ctree.add(cnodes);
                        }
                        nodes.put("checkArr", isPCheck==0?"0":"1");
                        nodes.put("children", ctree);
                    }
                    tree.add(nodes);
                }
                return tree;
            }else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
